package com.huixue.cn.ztej.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.util.StringUtil;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.TStudentExercises;
import com.huixue.cn.ztej.persistence.Domain;
import com.huixue.cn.ztej.service.TStudentExercisesService;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.framwork.PageParameter;

@Domain(TStudentExercises.class)
@Controller
@RequestMapping(value = "/sys/tstudentExercises")
public class TStudentExercisesController {
	@Autowired(required = true)
	private TStudentExercisesService tStudentExercisesService;
	
	@RequestMapping(value = "/list")
	public String list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = PageParameter.DEFAULT_PAGE_SIZE+"") Integer rows,
			TStudentExercises searchForm,Model model,HttpServletRequest request) {
		Map<String,Object> searchParams = new HashMap<String,Object>();
		if(StringUtil.isNotEmpty(searchForm.getStudentname())){
			searchParams.put("studentname", searchForm.getStudentname());
		}
		if(searchForm.getSubjectId()!=null){
			searchParams.put("subjectId", searchForm.getSubjectId());
		}
		
		Results results = tStudentExercisesService.searchByPage(searchParams,page,rows);
		model.addAttribute("res", results);
		model.addAttribute("searchForm", searchForm);
		return "/tstudentExercises/list";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		return "/tstudentExercises/add";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate/{id}")
	public String toUpate(@PathVariable ("id")Integer id,Model model,HttpServletRequest request) {
		TStudentExercises info = tStudentExercisesService.getInfo(id);
		model.addAttribute("student", info);
		return "/tstudentExercises/update";
	}
	
	
	/**
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/udpate",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results udpate(@RequestBody TStudentExercises  info,Model model,HttpServletRequest request) {
		tStudentExercisesService.updateByPrimaryKeySelective(info);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "修改成功！");
		return results;
	}

	@ResponseBody
	@RequestMapping(value = "/add",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results add(@RequestBody TStudentExercises info,HttpServletRequest request) {
		tStudentExercisesService.save(info);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "保存成功！");
		return results;
	}
	
	@ResponseBody
	@RequestMapping(value = "/save",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results save(@RequestBody TStudentExercises info,HttpServletRequest request) {
		TStudentExercises currentInfo = tStudentExercisesService.getInfo(info.getId());
		tStudentExercisesService.update(currentInfo);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "保存成功！");
		return results;
	}
	

}
